/**
 * 系统平台
 * moonvip_admin
 * com.tz.web.goodcatogory
 * GoodcatogoryController.java
 * 创建人:小胡
 * 时间：2016年12月06日 22:54:38
 *
 */
package com.tz.web.goodcatogory;

import com.tz.auth.AuthPassport;
import com.tz.bean.Goodcatogory;
import com.tz.bean.TzParams;
import com.tz.service.good.IGoodService;
import com.tz.service.goodcatogory.IGoodcatogoryService;
import com.tz.util.StringHelper;
import com.tz.util.TmFileUtil;
import com.tz.util.TmStringUtils;
import com.tz.util.TzConstant;
import com.tz.util.qiniuUtils.QiniuParamter;
import com.tz.util.qiniuUtils.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 平台商品分类管理web
 * GoodcatogoryController
 * 创建人:小胡
 * 时间：2016年12月06日 22:54:38
 * @version 1.0.0
 * 
 */
@Controller
@RequestMapping("/admin/shop/goodcatogory")
public class GoodcatogoryController {
	
	@Autowired
	private IGoodcatogoryService goodcatogoryService;
	private IGoodService goodService;

	private final  static  String filePath =   TzConstant.PATH_GOOD_PATH;
	private final  static  String key_suff =   "shop/goodcatogory/";


	/*列表查询*/
	@AuthPassport
	@RequestMapping("/list")
	public String list(TzParams params){
		return "shop/goodcatogory/list";
	}

    /*模板页面*/
	@RequestMapping("/template")
	public String template(TzParams params ,Model model){
		params.setDeleteCode("001");
		model.addAttribute("datas", goodcatogoryService.findGoodcatogorys(params));
		model.addAttribute("itemCount", goodcatogoryService.count(params));
		return "shop/goodcatogory/template";
	}

	@AuthPassport
	@RequestMapping(value = "/add" ,method = RequestMethod.GET)
	public String add(Model model){
		List<HashMap<String, Object>> firstcatogorys = goodcatogoryService.getFirstCatogary();
		model.addAttribute("firstcatogorys", firstcatogorys);
		return "shop/goodcatogory/add";
	}

	@AuthPassport
	@RequestMapping(value = "/edit/{id}" ,method = RequestMethod.GET)
	public String edit(@PathVariable("id") Integer id ,Model model){
		HashMap<String,Object>  goodcatogory= goodcatogoryService.getGoodcatogory(id);
		model.addAttribute("goodcatogory",goodcatogory);
		List<HashMap<String, Object>> firstcatogorys = goodcatogoryService.getFirstCatogary();
		model.addAttribute("firstcatogorys", firstcatogorys);
		return "shop/goodcatogory/edit";
	}

	@AuthPassport
	@ResponseBody
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public String delete(TzParams params){
		goodcatogoryService.delete(params);
		return "success";
	}

	@RequestMapping(value = "/save" ,method = RequestMethod.POST)
	public String save(HttpServletRequest request ,Goodcatogory goodcatogory) throws IOException {
		String path = StringHelper.upload(request  , filePath);
		String key = key_suff +  goodcatogory.getId() + "/"+path.substring(path.lastIndexOf("/")+1);
		QiniuUtil.upload(path , key);
		String bannerurl = QiniuParamter.URL + key;
		goodcatogory.setBannerurl(bannerurl);
		goodcatogory.setDeleteCode("001");
		goodcatogoryService.save(goodcatogory);
		return  "redirect:list";
	}



	@RequestMapping(value="/update",method= RequestMethod.POST)
	public String update(HttpServletRequest request , Goodcatogory goodcatogory) throws IOException {
		String path = StringHelper.upload(request  , filePath);
		if(TmStringUtils.isNotEmpty(path)){
			String key = key_suff +  goodcatogory.getId() + "/"+path.substring(path.lastIndexOf("/")+1);
			//URLEncoder.encode(s, "utf-8"); 如果有汉字，需要对key进行编码
			QiniuUtil.upload(path , key);
			String bannerurl = QiniuParamter.URL + key;
			goodcatogory.setBannerurl(bannerurl);
			TmFileUtil.delete(path);
		}
		if(goodcatogory.getLevel().equals("一级分类")){
			goodcatogory.setPid(null);
		}
		goodcatogory.setUpdateTime(new Date());
		goodcatogoryService.update(goodcatogory);
		return  "redirect:list";
	}

	@ResponseBody
	@RequestMapping(value = "/checkSecondname")
	public int  checkSecondname(TzParams params){
		return  goodcatogoryService.getCountBysecondName(params);
	}

	@ResponseBody
	@RequestMapping(value = "/checkFirstname")
	public int  checkFirstname(TzParams params){
		return  goodcatogoryService.getCountByfirstName(params);
	}


	/*商品分类列表查询*/
	@ResponseBody
	@RequestMapping("/root")
	public HashMap<String,Object> root(TzParams params){
		return goodcatogoryService.findCatogories(params);
	}
}
